package com.erp.erp_ui.UserGroup;

import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.UserGroup.UserGroupEntity;
import com.erp.erp_entitys.UserGroup.UserGroupEntityExample;
import com.erp.erp_servers.UserGroup.IUserGroupService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName UserGroupControl
 * @Description 用户组
 * @Author songjizhen
 * @Date 2020-07-04 8:54
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "usergroup")
 public class UserGroupControl extends Base {
    @Autowired
   IUserGroupService userGroupService;
    /**
     * 新增用户组
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody UserGroupEntity record) {
        try {
            int num=0;
            if(!isExist(record.getCode())){
                UserEntity user = (UserEntity)this.getBean("user");
                record.setCreator(user.getCode());
                record.setCreateTime(new Date());
                 num = userGroupService.insertSelective(record);
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 删除用户组
     * @param codes
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
     public R<Integer> delete(String[] codes){
        try {
            List<String> list=new ArrayList<String>();
            for(int i=0;i<codes.length;i++){
                list.add(codes[i]);
            }
            UserGroupEntityExample example=new UserGroupEntityExample();
            UserGroupEntityExample.Criteria criteria = example.createCriteria();
            criteria.andCodeIn(list);
            int num  = userGroupService.deleteByExample(example);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据条件查询用户组
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
     public  R<List<UserGroupEntity> > selectByPage() {//,String pageNum,String pageSize@RequestBody UserGroupEntity record
        try {
            UserGroupEntity record=new UserGroupEntity();
           List<UserGroupEntity> userGroupEntities = userGroupService.selectByExample(record);
            return R.build(userGroupEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 根据主键更新用户组
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody UserGroupEntity record) {
        try {
            record.setId(getByCode(record.getCode()));
            int num = userGroupService.updateByPrimaryKeySelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    private int getByCode(String code){
        UserGroupEntity uge=new UserGroupEntity();
        uge.setCode(code);
        List<UserGroupEntity> userGroupEntitys = userGroupService.selectByExample(uge);
        return  userGroupEntitys.get(0).getId();
    }
    private boolean isExist(String code){
        UserGroupEntity uge=new UserGroupEntity();
        uge.setCode(code);
        List<UserGroupEntity> userGroupEntitys = userGroupService.selectByExample(uge);
        return  userGroupEntitys.size()==1;
    }
/**
     * 根据主键查询用户组
     * @param id
     * @return
     */
    @RequestMapping(value = "selectByKey",method = RequestMethod.POST)
    @ResponseBody
     public R<UserGroupEntity> selectByKey(Integer id) {
        try {
            UserGroupEntity userGroupEntity = userGroupService.selectByPrimaryKey(id);
            return R.build(userGroupEntity);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "selectByCode")
    @ResponseBody
    public R<UserGroupEntity> selectByCode(String code) {
        try {
            UserGroupEntity uge=new UserGroupEntity();
            uge.setCode(code);
            List<UserGroupEntity> userGroupEntitys = userGroupService.selectByExample(uge);
            return R.build(userGroupEntitys.get(0));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获取所有用户组
     * @return
     */
    @RequestMapping(value = "selectAll")
    @ResponseBody
    public  R<List<UserGroupEntity>> selectAll(){
        try {
            UserGroupEntity uge=new UserGroupEntity();
           // uge.setCode(code);
            List<UserGroupEntity> userGroupEntitys = userGroupService.selectByExample(uge);
            return R.build(userGroupEntitys);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
}
